﻿using furion.blog.Application.Contracts;
using furion.blog.Domain.Shared;
using furion.blog.Utils;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Mvc;
using Furion.UnifyResult;
using furion.blog.Domain.Shared.Dto.User;
using Furion.FriendlyException;
using furion.blog.Domain.Shared.Enum;
using DotNetCommon.Encrypt;
using Furion;
using furion.blog.Domain.Shared.Vo.User;
using furion.blog.Domain.Shared.Attribute;
using Microsoft.AspNetCore.Authorization;
using Collections.Pooled;
using furion.blog.Domain.Query.User;

namespace furion.blog.Web.Entry.Api
{
    [ApiDescriptionSettings(Groups = new string[] { "UserApi" })]
    [Route("api/user")]
    public class UserApiController : IDynamicApiController
    {
        // 日志对象
        private readonly ILogger<UserApiController> _logger;
        private readonly ISystemService _systemService;
        private readonly IBaseInfoService _baseInfoService;
        private readonly IUserService _userService;

        public UserApiController(
            ISystemService systemService,
            ILogger<UserApiController> logger,
            IBaseInfoService baseInfoService,
            IUserService userService
            )
        {
            _systemService = systemService;
            _logger = logger;
            _baseInfoService = baseInfoService;
            _userService = userService;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [ActionAttribute(Name = "后台登录接口", Desc = "后台账号登录")]
        [HttpPost, Route("login"), AllowAnonymous]
        public async Task<IActionResult> login([FromBody] UserLoginDto dto)
        {
            UserLoginSuccessVO vo = await this._userService.Login(dto);
            return new OkObjectResult(ResultUtil.Success(vo));
        }

        /// <summary>
        /// 当前登录账号信息
        /// </summary>
        /// <returns></returns>
        [ActionAttribute(Name = "账号信息接口", Desc = "当前登录的后台账号信息")]
        [HttpGet, Route("info"), Authorize]
        public async Task<IActionResult> info([FromHeader(Name = "UserId")] long id)
        {
            CurrentLoginUserInfoVO vo = await this._userService.CurrentLoginUserInfo(id);
            return new OkObjectResult(ResultUtil.Success(vo));
        }

        /// <summary>
        /// 当前登录账号菜单
        /// </summary>
        /// <returns></returns>
        [ActionAttribute(Name = "账号拥有菜单接口", Desc = "当前登录的后台账号拥有的菜单")]
        [HttpGet, Route("menu"), Authorize]
        public async Task<IActionResult> menus([FromHeader(Name = "UserId")] long id)
        {
            var result = await _userService.CurrentUserMenus(id);
            return new OkObjectResult(ResultUtil.Success(result));
        }


        /// <summary>
        /// 当前登录账号退出系统
        /// </summary>
        /// <returns></returns>
        [ActionAttribute(Name = "账号退出登录接口", Desc = "当前登录的后台账号退出系统")]
        [HttpPost, Route("logout"), AllowAnonymous]
        public async Task<IActionResult> logout([FromHeader(Name = "UserId")] long id)
        {
            return new OkObjectResult(ResultUtil.Success(null));
        }

        [ActionAttribute(Name = "修改密码接口", Desc = "当前登录的后台账号修改密码")]
        [HttpPost, Route("changePassword")]
        public async Task<IActionResult> changePassword([FromBody] UserChangePasswordDto dto, [FromHeader(Name = "UserId")]long userId)
        {
            dto.UserId = userId;
            await _userService.ChangePassword(dto);
            return new OkObjectResult(ResultUtil.Success());
        }

        [ActionAttribute(Name = "账号信息接口", Desc = "当前登录的后台账号个人信息")]
        [HttpGet, Route("profile")]
        public async Task<IActionResult> profile([FromHeader(Name = "UserId")] long id)
        {
            var vo = await this._userService.Profile(id);
            return new ObjectResult(ResultUtil.Success(vo));
        }

        [ActionAttribute(Name = "账号信息更新接口", Desc = "当前登录的后台账号个人信息更新")]
        [HttpPost, Route("profile/update")]
        public async Task<IActionResult> updateProfile([FromBody] UserProfileUpdateDto dto, [FromHeader] long userId)
        {
            dto.UserId = userId;
            dto.UpdatedBy = userId;
            await this._userService.UpdateProfile(dto);
            return new ObjectResult(ResultUtil.Success());
        }

        [ActionAttribute(Name = "后台管理账号分页接口", Desc = "后台管理账号分页数据")]
        [HttpGet, Route("list")]
        public async Task<IActionResult> list(
            [FromQuery(Name = "page")] int pageIndex,
            [FromQuery(Name = "limit")] int pageSize,
            [FromQuery(Name = "searchContent")] string searchContent = "",
            [FromQuery(Name = "status")] string status = "",
            [FromQuery(Name = "roleId")] long? roleId = null,
            [FromQuery(Name = "deptId")] long? deptId = null
            )
        {
            UserListQuery query = new UserListQuery
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                SearchContent = searchContent,
                IsDelete = YesOrNoEnum.No.ToString(),
                Status = status,
                RoleId = roleId,
                DeptId = deptId,
            };
            var ret = await this._userService.List(query);
            return new OkObjectResult(ResultUtil.Success(ret));
        }


        [ActionAttribute(Name = "修改后台管理账号状态接口", Desc = "修改后台管理账号状态")]
        [HttpPost, Route("{id}/{status}")]
        public async Task<IActionResult> changeStatus(
            [FromRoute(Name = "id")] long id,
            [FromRoute(Name = "status")] string status,
            [FromHeader(Name = "UserId")] long userId
            )
        {

            return new OkObjectResult(ResultUtil.Success());
        }

        [ActionAttribute(Name = "后台管理账号删除接口", Desc = "删除后台管理账号")]
        [HttpDelete, Route("{id}")]
        public async Task<IActionResult> delete([FromRoute(Name = "id")] long id, [FromHeader(Name = "UserId")] long userId)
        {
            await this._userService.Delete(id, userId);
            return new OkObjectResult(ResultUtil.Success());
        }

        [ActionAttribute(Name = "后台管理账号恢复原始密码接口", Desc = "恢复后台管理账号原始密码")]
        [HttpPost, Route("password/recover/{id}")]
        public async Task<IActionResult> recoverPassword([FromRoute(Name = "id")] long id, [FromHeader(Name = "UserId")] long userId)
        {
            await this._userService.PasswordRecover(id, userId);
            return new OkObjectResult(ResultUtil.Success());
        }


        [ActionAttribute(Name = "后台管理账号信息接口", Desc = "获取后台管理账号详细信息")]
        [HttpGet, Route("{id}")]
        public async Task<IActionResult> detail([FromRoute(Name = "id")] long id)
        {
            var ret = await this._userService.Detail(id);
            return new OkObjectResult(ResultUtil.Success(ret));
        }


        [ActionAttribute(Name = "新增后台管理账号接口", Desc = "新增后台管理账号信息")]
        [HttpPost, Route("add")]
        public async Task<IActionResult> add([FromBody]UserCreateDto dto,[FromHeader(Name = "UserId")]long userId) {
            dto.CreatedBy = dto.UpdatedBy = userId;
            await this._userService.Add(dto);
            return new OkObjectResult(ResultUtil.Success());
        }

        [ActionAttribute(Name = "更新后台管理账号接口", Desc = "更新后台管理账号信息")]
        [HttpPost, Route("update")]
        public async Task<IActionResult> update([FromBody]UserUpdateDto dto,[FromHeader(Name = "UserId")]long userId) {
            dto.UpdatedBy = userId;
            await this._userService.Update(dto);
            return new OkObjectResult(ResultUtil.Success());
        }


        [HttpGet, Route("test"), AllowAnonymous]
        public async Task<ResultVO> test()
        {
            long id = IdUtil.Id();
            return ResultUtil.Success(DESEncrypt.Encrypt("21218cca77804d2ba1922c33e0151105", App.Configuration["SecretKey"]));
        }

    }
}
